<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="form" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="form.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="form.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  v-model="form.mobile"
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <selectTree v-model="form.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="form.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="form.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="form.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <ImgAdd v-model="form.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="handleSubmit">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import selectTree from './components/select-tree.vue'
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
import ImgAdd from './components/img-add.vue'

export default {
  components: { selectTree, ImgAdd },
  data() {
    return {
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentId: '', // 部门Id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: '' // 员工头像
      },
      rules: {
        // 姓名验证
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        // 手机号验证
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        // 部门验证
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        // 聘用形式验证
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        // 入职时间验证
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        // 转正时间验证
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (this.form.timeOfEntry) {
              if (+new Date(value) >= +new Date(this.form.timeOfEntry)) {
                callback()
                return
              } else {
                callback(new Error('转正时间不能早于入职时间'))
              }
            }
          }, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取动态路由参数
    console.log(this.$route.params.id)
    // 判断是否有id 如果有才获取数据
    if (this.$route.params.id) {
      this.getDepartment()
    }
  },
  methods: {
    async getDepartment() {
      this.form = await getEmployeeDetail(this.$route.params.id)
    },
    async handleSubmit() {
      await this.$refs.userForm.validate() // 验证表单
      if (this.$route.params.id) {
        // 如果id存在的情况下则是编辑状态需要发送不同的请求
        await updateEmployee(this.form)
        // 提示用户
        this.$message.success('修改员工成功')
      } else {
        // 发送请求
        await addEmployee(this.form)
        // 重置表单
        this.$refs.userForm.resetFields()
        // 提示用户
        this.$message.success('添加员工成功')
      }
      // 跳转回列表页
      this.$router.push('/employee')
    }
  }
}
</script>

  <style scoped lang="scss">
      .edit-form {
        background: #fff;
        padding: 20px;
        .inputW {
          width: 380px
        }
      }

  </style>
